//
//  UIView+UIView_ITAdditions.m
//  fTalk
//
//  Created by 王方帅 on 13-6-14.
//
//

#import "UIView+UIView_ITAdditions.h"

@implementation UIView (UIView_ITAdditions)

//纵坐标和宽高不变，只修改横坐标，以下分别修改一项
-(void)setFrame_x:(CGFloat)x
{
    [self setFrame:CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)];
}
-(void)setFrame_y:(CGFloat)y
{
    [self setFrame:CGRectMake(self.frame.origin.x, y, self.frame.size.width, self.frame.size.height)];
}
-(void)setFrame_width:(CGFloat)width
{
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, width, self.frame.size.height)];
}
-(void)setFrame_height:(CGFloat)height
{
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height)];
}

- (CGFloat)getFrame_x
{
    return self.frame.origin.x;
}

- (CGFloat)getFrame_y
{
    return self.frame.origin.y;
}

- (CGFloat)getFrame_height
{
    return self.frame.size.height;
}

- (CGFloat)getFrame_width
{
    return self.frame.size.width;
}

-(CGFloat)getFrame_right
{
    return self.frame.origin.x + self.frame.size.width;
}

-(CGFloat)getFrame_Bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

-(void)setSize:(CGSize)size
{
    CGRect rect = self.frame;
    rect.size = size;
    self.frame = rect;
}

-(CGSize)size
{
    return self.frame.size;
}

-(CGPoint)origin
{
    return self.frame.origin;
}

- (CGFloat)getCenterX
{
    return self.center.x;
}

- (CGFloat)getCenterY
{
    return self.center.y;
}

- (void)setCenterX:(CGFloat)x
{
    [self setCenter:CGPointMake(x, [self getCenterY])];
}

- (void)setCenterY:(CGFloat)y
{
    [self setCenter:CGPointMake([self getCenterX], y)];
}

+(CGSize)viewMakeUpWithArray:(NSArray *)array containsize:(CGRect)rect
{
    int height = 0;
    for (UIView *view in array)
    {
        if (view.size.height > height)
        {
            height = view.size.height;
        }
    }
    int centerY = rect.origin.y + height/2;
    UIView *precedingView = nil;
    for (UIView *view in array)
    {
        [view setCenter:CGPointMake(view.center.x, centerY)];
        if ([view isKindOfClass:[UILabel class]])
        {
            UILabel *label = (UILabel *)view;
            [label setFrame_width:[label.text sizeWithFont:label.font].width];
        }
        if (precedingView != nil)
        {
            int right = [precedingView getFrame_right]+view.size.width;
            if (right >= (rect.origin.x+rect.size.width))
            {
                [view setCenter:CGPointMake(view.center.x, precedingView.center.y+height)];
                [view setFrame_x:rect.origin.x];
            }
            else
            {
                [view setCenter:CGPointMake(view.center.x, precedingView.center.y)];
                [view setFrame_x:[precedingView getFrame_right]];
            }
        }
        precedingView = view;
    }
    UIView *view = [array lastObject];
    if (view.center.y != centerY)
    {
        return CGSizeMake(rect.size.width, [view getFrame_Bottom]);
    }
    else
    {
        return CGSizeMake([view getFrame_right], [view getFrame_Bottom]);
    }
}

- (void)setFrameXYZeroAndSize:(CGSize)viewSize
{
    [self setFrame:CGRectMake(0, 0, viewSize.width, viewSize.height)];
}

- (void)setXYZero
{
    [self setFrame_x:0];
    [self setFrame_y:0];
}

@end
